package com.golf.client.ui.awt;

import java.awt.Canvas;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import com.golf.client.course.renderer.CompositeRenderer;
import com.golf.client.course.renderer.CourseRenderer;
import com.golf.client.course.renderer.InteractivePlayerRenderer;
import com.golf.client.player.InteractivePlayer;
import com.golf.client.ui.MainLoop;
import com.golf.core.course.Course;
import com.golf.core.course.bodies.Ball;
import com.golf.core.course.exceptions.CourseLoadException;
import com.golf.core.course.loader.CourseLoader;
import com.golf.core.player.Player;
import com.golf.server.engine.phys.Engine;
import com.golf.server.game.Game;
import com.golf.server.game.GameRules;

/**
 * 
 * 
 * @author gfloodgate
 */
public class GolfApp extends Frame
{

	/** <code>serialVersionUID</code> for this class. */
	private static final long serialVersionUID = -5944315782373103029L;


	/**
	 * Create an instance of the application.
	 * @throws CourseLoadException 
	 */
	public void start() throws CourseLoadException
	{
		Canvas canvas = new Canvas();
		canvas.setSize(600, 300);

		add(canvas);
		setSize(600,300);
		
		setVisible(true);
	    addWindowListener(new WindowAdapter() 
	    {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
	    
        Course c = CourseLoader.load("course.xml");
	    final Engine e = new Engine(c);
	      
        
        InteractivePlayer p = new InteractivePlayer("jim", e, new Ball("playerball", c.getStartPosition()));
		canvas.addMouseListener(p);
		canvas.addMouseMotionListener(p);
		canvas.addMouseWheelListener(p);
		
		// Build the list of players - again this willPlayerually go away ....
		final List<Player> players = new ArrayList<Player>();
		players.add(p);
		
        
		
        // **********************

		// Create and start the game server (NB: this will be replaced with "attach to game server channel" ......)
		// TODO: Replace me with client to game server connection code ...
        final GameRules gameRules = new GameRules(3, 4); // 3 games, 2 shots each.
        
        final Game game = new Game(players, gameRules);
        
        //game.registerMotionListener();
        
        final Thread t = new Thread(game);
        t.start();		
		
        // **********************
        
        
		// Create the renderer(s) for this client.
		final CompositeRenderer renderer = new CompositeRenderer(canvas);
		renderer.addLayerRenderer(new CourseRenderer(c));
		renderer.addLayerRenderer(new InteractivePlayerRenderer(p));

		// Start the main rendering/update loop.
		final Thread loop = new Thread(new MainLoop(e, renderer));
		loop.setPriority(Thread.NORM_PRIORITY);
		loop.start();
	}
	
	
	/**
	 * The command line entry point. 
	 * 
	 * @param args The command line arguments passed to the server.
	 * @throws CourseLoadException 
	 */
	public static final void main(final String[] args) throws CourseLoadException
	{
		final GolfApp app = new GolfApp();
		app.start();
	}
	
}
